home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / IntVec.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  6KB  |  207 lines

  1. #ifndef INTVEC_H
  2. #define INTVEC_H
  3. #pragma once
  4.  
  5. /*
  6.  *    Declarations for Integer vectors
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)IntVec.h    2.1    8/18/89
  27.  */
  28.  
  29. /*    This code is designed to be as compatible as possible with the
  30.  *    NIH Vector classes, while preserving efficiency.  These Vectors
  31.  *    are NOT based on the NIH "Object" class, making them much
  32.  *    smaller.  They also implement reference counting, making them
  33.  *    faster. 
  34.  */
  35.  
  36.  
  37. #include "vdefs.h"
  38. #include <math.h>
  39.  
  40. class istream;
  41. class ostream;
  42. class IntVec;
  43. class DoubleVec;
  44. class FloatVec;
  45.  
  46. class IntBlock {
  47.   unsigned short      refs;        // Number of references
  48.   unsigned          npts;        // Number of elements
  49.   int            array[1];    // The data
  50.   friend        IntVec;
  51. public:
  52.   IntBlock(unsigned n);
  53.   IntBlock(unsigned n, int val);
  54.   IntBlock(unsigned n, int val, int by);
  55.   ~IntBlock();
  56.  
  57.   void            add_reference()    {refs++;}
  58.   unsigned        references()    {return refs;}
  59.   int*            data()        {return array;}
  60. };
  61.  
  62. class IntVec {
  63.   IntBlock*        block;
  64.   int*            begin;
  65.   unsigned        npts;
  66.   int            step;
  67.  
  68.   static int        numberPerLine; // For printing
  69.   IntVec(IntVec&, int, unsigned, int); // For slices
  70. protected:
  71.   void            boundsErr(int);
  72.   void            boundsCheck(int);
  73.   void            lengthErr(int);
  74.   void            lengthCheck(int i)    {if(npts!=i) lengthErr(i);}
  75.   void            emptyErr(const char* fname);
  76.   void            sliceErr(unsigned, int, unsigned, int);
  77. public:
  78.   IntVec();
  79.   IntVec(unsigned n);
  80.   IntVec(unsigned n, int val);
  81.   IntVec(unsigned n, int val, int by);
  82.   IntVec(const IntVec& a);
  83.   IntVec(const int* dat, unsigned n);  // Copy of dat will be made
  84.   ~IntVec();
  85.   
  86.   // Conversion:
  87.   operator        DoubleVec();    // Convert to DoubleVec
  88.   operator        FloatVec();    // Convert to FloatVec
  89.  
  90.   IntVec        slice(int start, unsigned lgt, int strider=1);
  91.   
  92.   int*            data()        {return begin;}
  93.   unsigned        length()    {return npts;}
  94.   int            stride()    {return step;}
  95.  
  96.   IntVec&        reference(IntVec& v);    // Reference self to v
  97.   IntVec        deepCopy();    // copy of self with distinct instance variables 
  98.   IntVec        copy()        {return deepCopy();} // Synonym for deepCopy()
  99.   void            deepenShallowCopy();    // Insures only 1 reference to data
  100.   void            resize(unsigned);    // Will pad with zeroes if necessary
  101.  
  102.   void            scanFrom(istream& s); // Read to eof
  103.   void            printOn(ostream& s);  // Pretty print
  104.   void            setFormatting(int);   // Change # items per line
  105.   
  106.   // Indexing:
  107.   int&            operator[](int i);    // With bounds checking
  108.   int&            operator()(int i);    // With optional bounds checking
  109.   
  110.   // Assignment:
  111.   IntVec&        operator=(const IntVec& v); // Must be same length as v
  112.   IntVec&        operator=(int);
  113.   
  114.   // Arithmetic operators:
  115.   IntVec&        operator++();
  116.   IntVec&        operator--();
  117.   IntVec&        operator+=(const IntVec&);
  118.   IntVec&        operator+=(int);
  119.   IntVec&        operator-=(const IntVec&);
  120.   IntVec&        operator-=(int);
  121.   IntVec&        operator*=(const IntVec&);
  122.   IntVec&        operator*=(int);
  123. //IntVec&        operator/=(const IntVec&);
  124. //IntVec&        operator/=(int);
  125.   
  126.   // Friendly arithmetic operators:
  127.   friend IntVec    operator-(const IntVec&);
  128.   friend IntVec    operator+(const IntVec&);
  129.   friend IntVec    operator*(const IntVec&,const IntVec&);
  130. //friend IntVec    operator/(const IntVec&,const IntVec&);
  131.   friend IntVec    operator+(const IntVec&,const IntVec&);
  132.   friend IntVec    operator-(const IntVec&,const IntVec&);
  133.   friend IntVec    operator*(const IntVec&,int);
  134.   friend IntVec    operator*(int,const IntVec&);
  135. //friend IntVec    operator/(const IntVec&,int);
  136. //friend IntVec    operator/(int,const IntVec&);
  137.   friend IntVec    operator+(const IntVec&,int);
  138.   friend IntVec    operator+(int,const IntVec&);
  139.   friend IntVec    operator-(const IntVec&,int);
  140.   friend IntVec    operator-(int,const IntVec&);
  141.   
  142.   
  143. #ifndef NO_VECTOR_MATHFUN
  144.   // Math functions:
  145.   IntVec    apply(mathFunTy);
  146.   friend    IntVec    abs(const IntVec&);
  147. //friend    IntVec    acos(const IntVec&);
  148. //friend    IntVec    asin(const IntVec&);
  149. //friend    IntVec    atan(const IntVec&);
  150. //friend    IntVec    atan2(const IntVec&,const IntVec&);
  151. //friend    IntVec    ceil(const IntVec&);
  152. //friend    IntVec    cos(const IntVec&);
  153. //friend    IntVec    cosh(const IntVec&);
  154.   friend    IntVec    cumsum(const IntVec&);
  155.   friend    IntVec    delta(const IntVec&);
  156.   friend    int    dot(const IntVec&,const IntVec&);
  157. //friend    IntVec    exp(const IntVec&); 
  158. //friend    IntVec    floor(const IntVec&);
  159. //friend    IntVec    log(const IntVec&);
  160.   friend    int    max(const IntVec&);
  161.   friend    int    min(const IntVec&);
  162. //friend    int    mean(const IntVec&);
  163.   friend    int    prod(const IntVec&);
  164. //friend    IntVec    pow(const IntVec&,const IntVec&);
  165.   friend    IntVec    reverse(const IntVec&);
  166. //friend    IntVec    rint(const IntVec&);
  167. //friend    IntVec    sin(const IntVec&);
  168. //friend    IntVec    sinh(const IntVec&);
  169. //friend    IntVec    sqrt(const IntVec&);
  170.   friend    int    sum(const IntVec&);
  171. //friend    IntVec    tan(const IntVec&);
  172. //friend    IntVec    tanh(const IntVec&);
  173. //friend    int    variance(const IntVec&);
  174.  
  175. // Int specific functions:
  176.   friend    IntVec    trunc(const DoubleVec&);// Truncate DoubleVec to IntVec
  177. #endif
  178.   
  179. };
  180.  
  181. // Other (related) declarations:
  182. ostream&    operator<<(ostream&, const IntVec&);
  183. istream&    operator>>(istream&, IntVec&);
  184.  
  185. /******************* I N L I N E S **************************/
  186.  
  187. Inline void    IntVec::setFormatting(int i){numberPerLine = i;}
  188.  
  189. Inline void    IntVec::boundsCheck(int i){
  190.   if(i<0 || i>npts) boundsErr(i);
  191. }
  192. Inline int&    IntVec::operator[](int i){
  193.   boundsCheck(i); return begin[i*step];
  194. }
  195. Inline int&    IntVec::operator()(int i) {
  196. #if BOUNDS_CHECK    
  197.   boundsCheck(i);
  198. #endif
  199.   return begin[i*step];
  200. }
  201.  
  202. Inline IntVec    operator+(const IntVec& a)        {return a;}
  203. Inline IntVec    operator*(int a, const IntVec& b)    {return b*a;}
  204. Inline IntVec    operator+(int a, const IntVec& b)    {return b+a;}
  205.  
  206. #endif
  207.